package com.sxyc.weixin.controller;

import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.kit.LogKit;
import com.jfinal.plugin.activerecord.Db;
import com.sxyc.weixin.m2m.EditTerminalClient;
import com.sxyc.weixin.m2m.GetTerminalDetailsClient;
import com.sxyc.weixin.m2m.GetTerminalRatingClient;
import com.sxyc.weixin.m2m.QueueTerminalRatePlanClient;
import com.sxyc.weixin.m2m.RemoveRatePlan;
import com.sxyc.weixin.m2m.TerminalDetailsService;

public class M2mController extends Controller {

	public void index(){
		String iccid = getPara("iccid");
		terminal(iccid);
		Object result = TerminalDetailsService.getDetail(iccid);
		int n = GetTerminalRatingClient.GetTerminalRating(iccid);
		renderJson(result + " 充值 ：" + (n-1));
	}
	

	public int terminal(String iccid){
		if(iccid.charAt(7)!='5' && iccid.charAt(7)!='6'){
			LogKit.info("非陕西和内蒙卡！");
    		return -1;//暂时无法充值
    	}
		GetTerminalDetailsClient detail = new GetTerminalDetailsClient();
		String[] res = detail.GetTerminalDetail(iccid);
    	if(res != null && res[0] != null){
    		if("DEACTIVATED_NAME".equals(res[0])){
	    		LogKit.info("激活SIM卡！");//当前卡为已停用状态，充值即改为已激活状态
	    		EditTerminalClient edit = new EditTerminalClient();
	    		LogKit.warn("EditStatus…… iccid: "+iccid);
	    		return edit.EditStatus(iccid);
	    	}else if("ACTIVATED_NAME".equals(res[0])){
    			LogKit.info("追加队列套餐！ Icccid : "+iccid);//当前卡为已激活状态，充值即追加队列套餐
	    		QueueTerminalRatePlanClient queue = new QueueTerminalRatePlanClient();
	    		LogKit.warn("QueueRatePlan…… iccid: "+iccid);
	    		return queue.QueueRatePlan(iccid);
	    	}
    	}
    	return -2;//没有获取到SIM卡状态或状态不对！
	}
	 
	
	public void getTerminalDetail(){
		String iccid = getPara("iccid");
		GetTerminalDetailsClient detail = new GetTerminalDetailsClient();
		String[] res = detail.GetTerminalDetail(iccid);
    	System.out.println("status:"+res[0]+"  dataRemaining:"+res[1]);
    	if("ACTIVATED_NAME".equals(res[0])){
    	    System.out.println(GetTerminalRatingClient.GetTerminalRating(iccid));//如果设备已充值过，不通知
    	}
		renderText("OK@!");
	}
	
	public void getTerminalDetails(){
		Object result = TerminalDetailsService.getDetail(getPara("iccid"));
		int n = GetTerminalRatingClient.GetTerminalRating(getPara("iccid"));
		renderJson(result + " 充值 ：" + (n-1));
	}
	
	public void getTerminal(){
		TerminalDetailsService service = new TerminalDetailsService();
		Object result = service.getDetails(getPara("iccid"));
		renderJson(result);
	}
	
	public void getTerminalRating(){
		String iccid = getPara("iccid");
    	System.out.println(GetTerminalRatingClient.GetTerminalRating(iccid));
		renderText("OK@!");
	}
	
	public void checkUsers(){
		List<String> list = Db.query("select iccid from iccidmsg where `status` = 2");
		int count = 0;
		for (String iccid : list) {
			int res = GetTerminalRatingClient.GetTerminalRating(iccid);
	    	if(res > 1){
	    		count++;
	    		System.out.println(iccid +"  已充值！");
	    	}
		}
		renderJson("共 "+count+" 位用户已充值，但未激活！");
	}

	public void queueRatePlan(){
		String iccid = getPara("iccid");
    	QueueTerminalRatePlanClient queue = new QueueTerminalRatePlanClient();
    	System.out.println(queue.QueueRatePlan(iccid));
		renderText("OK@!");
	}
	
	public void RemoveRatePlan(){
		String iccid = getPara("iccid");
		String result = RemoveRatePlan.RemoveRatePlans(iccid,"1");
    	System.out.println(result);
		renderText(result);
	}
	
	public void pushSS(){
		String sql = " select iccid from deviceinfo";
		List<String> iccids = Db.query(sql);
		int i = 0;
		for (String iccid : iccids) {
			
			GetTerminalDetailsClient detail = new GetTerminalDetailsClient();
			String[] res = detail.GetTerminalDetail(iccid);
	    	System.out.println(res[0]+" "+res[1]);
	    	if(res != null && res[0] != null){
		    	System.out.println("激活SIM卡！");//当前卡为已停用状态，充值即改为已激活状态
		    	i = i + 1;
		    	EditTerminalClient edit = new EditTerminalClient();
		    	Integer count = edit.EditStatus(iccid);
		    	if(count > 0 ){
		    		sql = "update deviceinfo set isActive=1";
		    		Integer j = Db.update(sql);
		    		String result;
		    		if(j>0){
		    			 result = "true";
		    		}else{
		    			 result = "false";
		    		}
		    		
		    		System.out.println("激活第" +i+"条数据！" +   result );	
		    	}else{
		    		System.out.println(iccid + ":激活失败，请重新激活！");
		    		
		    	}
	    	}
		}
	}
}
